Interactive charts

Plotly

Styling a Plotly chart

# Wrangle data for plotting
iris_bar_df <- iris %>% 
  group_by(Species) %>% 
  summarise(Sepal_Width = mean(Sepal.Width)) %>% 
  ungroup()

# Create basic bar chart (left)
plotly_iris_bar <- plot_ly(iris_bar_df,
                    x = ~Species,
                    y = ~Sepal_Width,
                    type = "bar")

# Create formatted chart (right)
plotly_iris_bar_formatted <- plot_ly(iris_bar_df,
                    x = ~Species,
                    y = ~Sepal_Width,
                    type = "bar",
                    marker = list(color = nice_colours[["bold_teal_100"]],
                                  line = list(color = nice_colours[['black_100']], width = 1.5)))

plotly_iris_bar
plotly_iris_bar_formatted
plotly_iris_bar_themed <- plotly_iris_bar_formatted %>% 
  nice_plotly_theme(chart_type = "vertical_bar",
             x_title = "Species",
             y_title = "Sepal Width")

Formatted chart headline title

Figure x. Formatted chart statistical title

Alt text (text description of message chart is showing. Mark chart as decorative)
Source: []
Download the data for Figure x (CSV, 5.0KB).

How to save your plot

To download the plot as a static PNG image, click on the camera icon at the top right of the plot.

Saving in other file formats (e.g. .svg or .jpg) is more complicated and requires installation of another piece of software. Read the Plotly documentation on exporting graphs as static images in R.

Chart types

# Load in DOACs data
doacs_df <- readr::read_csv(here::here("DOACs_data.csv"),
                              col_types = "Dcccccdddd")

Histogram

Use chart_type = “horizontal_bar” for histograms.

# Create chart
plotly_hist <- iris %>%
  plot_ly(x = ~Sepal.Width,
          color = ~Species,
          # Use NICE colours - remove colour names so it maps correctly
          colors = unname(nice_palettes[["discrete"]][1:3]),
          type = "histogram",
          # Black outline for bars
          marker = list(line = list(color = nice_colours[['black_100']], width = 1.5)),
          # Set bin width
          xbins = list(size = 0.2,
                       start = 1.9,
                       end = 4.5)) %>% 
  # Stack bars
  layout(barmode = "stack") %>% 
  # Add NICE theme and set axis titles
  nice_plotly_theme(chart_type = "vertical_bar",
             x_title = "Sepal width",
             y_title = "Frequency")

plotly_hist
# Category order differs from ggplot but no big difference

Own tests

economics_long %>%
  filter(variable != "pop") %>%
  plot_ly(x = ~date,
         y = ~value01,
         color = ~variable,
         colors = c("#00436C", "#D07B4D", "#000000", "#37906D"),
         type = "scatter",
         mode = "lines") %>%
  nice_plotly_theme(chart_type = "line")
plot1 <- mpg %>%
  filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
  count(manufacturer) %>%
  plot_ly(y = ~manufacturer,
          x = ~n,
          color = ~manufacturer,
          type = "bar",
          orientation = "h",
          marker = list(line = list(color = '#000000', width = 1.5)))

nice_plotly_theme(plot1, chart_type = "horizontal_bar")
mpg %>%
  filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
  count(manufacturer) %>%
  plot_ly(y = ~manufacturer,
          x = ~n,
          color = ~manufacturer,
          type = "bar",
          orientation = "h",
          marker = list(line = list(color = '#000000', width = 1.5))) %>%
  nice_plotly_theme(chart_type = "horizontal_bar")
mpg %>%
  filter(manufacturer %in% c("hyundai", "nissan", "toyota")) %>%
  count(manufacturer) %>%
  plot_ly(x = ~manufacturer,
          y = ~n,
          color = ~manufacturer,
          type = "bar",
          marker = list(line = list(color = '#000000', width = 1.5))) %>%
  nice_plotly_theme(chart_type = "vertical_bar")